<?php
$loginUrl = 'https://itra.run/Account/Login';

$cookieFile = __DIR__ . '/cookie.txt';
if (file_exists($cookieFile)) {
    unlink($cookieFile);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
]);
$html = curl_exec($ch);
curl_close($ch);
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$tokenNode = $xpath->query('//input[@name="__RequestVerificationToken"]')->item(0);
if (!$tokenNode) {
    die("无法找到 __RequestVerificationToken");
}
$token = $tokenNode->getAttribute('value');

// print('==token==='.$token.'<br>');

$postFields = http_build_query([
    'Input.Email' => '465144122@qq.com',
    'Input.Password' => 'ChmLzj@123',
    '__RequestVerificationToken' => $token,
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
    'Referer: https://itra.run/Account/Login',
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);

// echo "HTTP 状态码：$httpCode"."<br>";

$Session = '';
$Application = '';

$cookieLines = file($cookieFile);
foreach ($cookieLines as $line) {
    if (strpos($line, '.AspNetCore.Session') !== false) {
       
        $Session = trim(explode("\t", $line)[6]);
    }
    if (strpos($line, '.AspNetCore.Identity.Application') !== false) {
        $Application = trim(explode("\t", $line)[6]);
    }
}

print('$Session='.$Session);
print("<br>");
print('$Application='.$Application);
print("<br>");

if (file_exists($cookieFile)) {
    unlink($cookieFile);
}

if (!$Session || !$Application) {
    die("未获取到完整的 Cookie 信息，无法发送。");
}

// 准备 POST 数据
$postData = http_build_query([
    'session' => $Session,
    'application' => $Application,
]);

// 发送 POST 请求到 saveauth.php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://zhizhangcs.cn/saveauth.php'); // 替换为你的实际地址
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded',
    'User-Agent: Mozilla/5.0'
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print('<br>');
echo "状态码: $httpCode\n";
print('<br>');
echo "响应内容:\n$response\n";

